package controller;

import domain.flower;
import execption.nullValueException;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import service.CustomerService;
import service.FlowerStoreService;
import service.impl.CustomerServiceImpl;
import service.impl.FlowerStoreServiceImpl;
import utill.ParamUtil;
import utill.component.promptBox;
import utill.switchpages;


import java.sql.SQLException;
import java.util.List;

public class StoreManage {

    @FXML
    private TextField GoutputIdText;

    @FXML
    private Label GoutputLabelone;

    @FXML
    private TextField GinputTextId;

    @FXML
    private HBox outputHbox;

    @FXML
    private TableColumn<?, ?> flowerPirce;

    @FXML
    private MenuItem btnInputcommit;

    @FXML
    private TableColumn<?, ?> flowerTotal;

    @FXML
    private Label OutputTitle;

    @FXML
    private Menu lookOrder;

    @FXML
    private TextField GoutputTotal;

    @FXML
    private Label GoutputLabeltwo;

    @FXML
    private GridPane GoutputFactory;

    @FXML
    private MenuItem btnShowOrder;

    @FXML
    private Menu AddFlower;

    @FXML
    private TextField GInputTableTextTotal;

    @FXML
    private TableColumn<?, ?> flowerType;

    @FXML
    private TableView<flower> orderTableView;

    @FXML
    private TableColumn<?, ?> flowerId;

    @FXML
    private Label outputTitle;

    @FXML
    private GridPane GinputFactory;

    @FXML
    private MenuItem btnOutputCommit;

    @FXML
    private MenuBar optionMenu;

    @FXML
    private MenuItem btnAddFlower;

    @FXML
    private Label GinputLabelTotal;

    @FXML
    private HBox inputHbox;

    @FXML
    private HBox titleHbox;

    @FXML
    private Menu outputFactory;

    @FXML
    private Menu inputfactory;

    @FXML
    private Label GinputLableId;

    @FXML
    private Menu btncallback;


    @FXML
    private AnchorPane ParentPane;

    CustomerService customerService =new CustomerServiceImpl();
    FlowerStoreService flowerStoreService =new FlowerStoreServiceImpl();

    String LoginUrl="/controller/login.fxml";

    String BreedUrl="/controller/breedFlower.fxml";

    String OrderUrl="/controller/storeSelectOrder.fxml";



    @FXML
    void inputCommit(ActionEvent event) {
        String id=GinputTextId.getText();
        String  total =GInputTableTextTotal.getText();
        int flowerId=0;
        int flowerTotal=0;
        if(!"".equals(id)){
            flowerId=Integer.valueOf(id);
        }
        if(!"".equals(total)){
            flowerTotal=Integer.valueOf(total);
        }

        try {

            flowerStoreService.InputFlower(flowerId,flowerTotal,true);
        } catch (nullValueException e) {
            e.printStackTrace();
            promptBox.tipsMessage(e.getMessage());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            promptBox.tipsMessage("跟新异常");
        }
        showData();
    }



    @FXML
    void selectOrder(ActionEvent event) throws Exception {
        switchpages switchpages =new switchpages();
        switchpages.switch_viewPane(this.ParentPane,this.OrderUrl);
    }

    @FXML
    void AddFlower(ActionEvent event) throws Exception {
        switchpages switchpages =new switchpages();
        switchpages.switch_viewPane(this.ParentPane,this.BreedUrl);
    }



    @FXML
    void outputCommit(ActionEvent event) throws Exception {
        String id=GoutputIdText.getText();
        String  total =GoutputTotal.getText();
        int flowerId=0;
        int flowerTotal=0;
        if(!"".equals(id)){
            flowerId=Integer.valueOf(id);
        }
        if(!"".equals(total)){
            flowerTotal=Integer.valueOf(total);
        }
        try {
            flowerStoreService.InputFlower(flowerId,flowerTotal);
        } catch (nullValueException e) {
            e.printStackTrace();
            promptBox.tipsMessage(e.getMessage());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            promptBox.tipsMessage("跟新异常");
        }
        showData();
    }


    @FXML
    void callback(ActionEvent event) throws Exception {
        ParamUtil.storeId.remove(Thread.currentThread().getName()+"storeId");
        switchpages switchpages =new switchpages();
        switchpages.switch_viewPane(this.ParentPane,this.LoginUrl);
    }

    @FXML
    void OnStoreFlower(ActionEvent event) {

        showData();
    }

    public void bindTableView(TableView  tableView ,  TableColumn column1 ,
                              TableColumn column2 ,TableColumn column3,TableColumn column4){
        column1.setCellValueFactory(new PropertyValueFactory<>("flowerId"));
        column2.setCellValueFactory(new PropertyValueFactory<>("flowerType"));
        column3.setCellValueFactory(new PropertyValueFactory<>("flowerPrice"));
        column4.setCellValueFactory(new PropertyValueFactory<>("flowertotal"));
    }

    public  void showData(){
        orderTableView.getItems().clear();
        List<flower> flowerList = customerService.findAllFlower(true);
        bindTableView(orderTableView,flowerId,flowerType,flowerPirce,flowerTotal);
        ObservableList<flower> data = FXCollections.observableArrayList();

        for(flower f : flowerList) {
            data.add(f);
        }
        orderTableView.setItems(data);
    }



}
